<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width: 200px;
            height: 200px;
            background-color: blueviolet;
            position: absolute;
        }
        div:nth-child(2){
            width: 200px;
            height: 200px;
            background-color: blueviolet;
            position: absolute;
            top: 200px;
        }
    </style>
</head>
<body>

    <div class="move"></div>
    <div class="movea"></div>
    <script>
        var move=document.querySelector(".move")
        var movea=document.querySelector(".movea")

        //callback&&callback()  如果有callback存在才能执行callback()  (callback为true才能执行callback())
        //callback||callback()  如果有callback存在就不执行callback()，如果没有callback存在就执行callback()


        function animate(obj,space,funtime,callback){
            var intervalID=setInterval(function(){
                var left=obj.offsetLeft;
                var speed=(space-left)/10  //速度（减速)
                speed=speed>0?Math.ceil(speed):Math.floor(speed)
                // speed=Math.ceil(speed)
                left+=speed;
                if(left==space)
                {
                    clearInterval(intervalID)
                    callback&&callback()
                }
                obj.style.left=left+'px'
            },funtime)
        }
        animate(move,800,20,function(){
            alert("动画执行完成了...")
        })
        animate(movea,-1,50,function(){
            alert("已经到达最左边了...")
        })
        


    </script>
    
</body>
</html>